using System;
using System.Text;
using System.Web.Security;
using RMH.Entities;
using RMH.Services;
using RMH.Services.MembershipProvider;

namespace RMH.Web.RMHMembership
{
    
    public class RMHMembershipProvider : System.Web.Security.MembershipProvider
    {
        private IStaffService staffService;

        public RMHMembershipProvider(IStaffService staffService)
        {
            this.staffService = staffService;
        }

        public RMHMembershipProvider()
        {
            staffService = Container.Resolve<IStaffService>();
        }

        public override MembershipUser CreateUser(string username, string password, string email, 
                                                  string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, 
                                                  out MembershipCreateStatus status)
        {
        
            var encryptedPassword = encrypt(password);

            var staff = new Staff { Email = email, Password = encryptedPassword, UserName = username };
            staffService.Save(staff);
            status = MembershipCreateStatus.Success;
            return staff.ToMemberShipUser();  // TODO: test this
        }

        private string encrypt(string password)
        {
            return Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            var user = staffService.GetByUserName(username);
            return user == null ? null : user.ToMemberShipUser();
        }

        public override bool ValidateUser(string username, string password)
        {
            return staffService.Validate(username, encrypt(password));
        }


        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            var staff = staffService.GetByUserName(username);
            // TODO: test this.
            if (staff.Password == encrypt(oldPassword))
            {
                staff.Password = encrypt(oldPassword);
                return true;
            }
            else
            {
                return false;
            }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 5; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return 1; }
        }

        #region NotImplemented

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }
        
        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }


        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }



        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }



        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override string ApplicationName
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }



        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        #endregion NotImplemented
                        
    }
}